package com.ltmonitor.web.controller.video;

import com.ltmonitor.entity.GPSRealData;
import com.ltmonitor.entity.MediaItem;
import com.ltmonitor.entity.TerminalCommand;
import com.ltmonitor.entity.VehicleData;
import com.ltmonitor.service.ILocationService;
import com.ltmonitor.service.IMediaItemService;
import com.ltmonitor.service.IRealDataService;
import com.ltmonitor.util.DateUtil;
import com.ltmonitor.web.controller.command.TerminalCommandAction;
import com.ltmonitor.web.util.JsonMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping("/videoCapture")
public class VideoCaptureAction extends TerminalCommandAction {

    /**
     * 位置服务
     */
    @Autowired
    private ILocationService locationService;
    @Autowired
    private IRealDataService realDataService;

    @Autowired
    private IMediaItemService mediaItemService;

    @ResponseBody
    @RequestMapping(value="/upload.action",method= RequestMethod.POST)
    public JsonMessage upload(String simNo, HttpServletRequest request,
                              MultipartFile Filedata, String Filename
                               )
            throws IllegalStateException, IOException {

        String[] strArray = simNo.split("_");

        simNo = strArray[0];
        simNo = simNo.length() > 11 ? simNo.substring(simNo.length() - 11) :simNo;

        int channelId = Integer.parseInt(strArray[1]);

        log.error(request.toString());

        if (!Filedata.isEmpty()) {
            String path = request.getSession().getServletContext().getRealPath("/") + "\\vehiclePicture\\";


            //创建多媒体上传记录
            MediaItem mi = new MediaItem();
            mi.setChannelId((byte)channelId);
            mi.setSimNo(simNo);

            VehicleData vd = this.vehicleDataService.getVehicleBySimNo(simNo);
            if (vd != null)
                mi.setPlateNo(vd.getPlateNo());

            GPSRealData rd = this.realDataService.get(simNo);

            try {
                if (rd != null) {
                    mi.setLatitude(rd.getLatitude());
                    mi.setLongitude(rd.getLongitude());
                    mi.setSpeed(rd.getVelocity());
                    mi.setSendTime(new Date());
                    if (locationService != null) {
                        String location = locationService.getLocation(mi.getLatitude(), mi.getLongitude());
                        mi.setLocation(location);
                    }

                }
            } catch (Exception ex) {
                mi.setSendTime(new java.util.Date());
                log.error("解析拍照发生异常" + ex.getMessage());
            }
            mi.setCommandType(MediaItem.USER);

            String extention = "";
            // 多媒体文件格式
            String fileFormat = "jpg";

            String fileName = mi.getPlateNo() + "_"
                    + DateUtil.toStringByFormat(new Date(), "yyyy-MM-dd-HH-mm-ss")
                    + "." + fileFormat;
            mi.setFileName(fileName);

            // 新的图片
            File newFile = new File(path + fileName);
            // 将内存中的数据写入磁盘
            Filedata.transferTo(newFile);
            try {
                mediaItemService.saveOrUpdate(mi);
            } catch (RuntimeException ex) {
                log.error(ex.getMessage(), ex);
            }

        }

        return json(true,"图片上传成功!");
    }

}
